package com.cssy.xxkj.logic.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicWhCheckTaskDAO;
import com.cssy.xxkj.logic.dao.LogicWhCheckTaskItemDAO;
import com.cssy.xxkj.logic.entity.LogicExcelToStoreDO;
import com.cssy.xxkj.logic.entity.LogicWhCheckTaskDO;
import com.cssy.xxkj.logic.entity.LogicWhCheckTaskItemDO;
import com.cssy.xxkj.logic.entity.vo.LogicWarehouseEndMonthSumVO;
import com.cssy.xxkj.logic.service.IFileUploadService;
import com.cssy.xxkj.logic.service.ILogicExcelToStoreService;
import com.cssy.xxkj.logic.util.OftenMapUtil;
import com.cssy.xxkj.logic.util.OftenMothedUtil;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;

@Service("fileUploadService")
public class FileUploadServiceImpl implements IFileUploadService {

	@Resource(name = "oftenMapUtil")
	private OftenMapUtil oftenMapUtil;
	
	@Resource(name = "oftenMothedUtil")
	private OftenMothedUtil oftenMothedUtil;
	
	@Resource(name="logicWhCheckTaskDAO")
    private LogicWhCheckTaskDAO logicWhCheckTaskDAO;
	
	@Resource(name="logicWhCheckTaskItemDAO")
    private LogicWhCheckTaskItemDAO logicWhCheckTaskItemDAO;
	
	@Resource(name = "logicExcelToStoreService")
	private ILogicExcelToStoreService logicExcelToStoreService;
	
	@Override
	public String fileInExcelStore(HttpServletRequest request) 
			throws SyBaseException {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartRequest.getFile("initExcel");
		//获得仓库的map
		Map<String , BasicStorageInfoTestDO> wareHouseMap= 
				this.oftenMapUtil.getStroageInfoMap(this.oftenMothedUtil.
						getCleanCSearchCondition(new SearchConditions()));
		HSSFWorkbook wb;
		Map<String, LogicExcelToStoreDO> excelMap= new HashMap<String, LogicExcelToStoreDO>();
		try {
			wb = new HSSFWorkbook(file.getInputStream());
			HSSFSheet sheet = wb.getSheetAt(0);
			for (int j = 1; j <= sheet.getLastRowNum(); j++){
				HSSFRow row = sheet.getRow(j);
				if(row == null ){
					continue;
				}
				
				String materCode=this.oftenMothedUtil.getStringCellValue(row.getCell(0));
				String materName=this.oftenMothedUtil.getStringCellValue(row.getCell(1));
				String materModel=this.oftenMothedUtil.getStringCellValue(row.getCell(2));
				String materSpec=this.oftenMothedUtil.getStringCellValue(row.getCell(3));
				String materColor=this.oftenMothedUtil.getStringCellValue(row.getCell(4));
				String materQuanlity=this.oftenMothedUtil.getStringCellValue(row.getCell(5));
				String storageName=this.oftenMothedUtil.getStringCellValue(row.getCell(17));
				BasicStorageInfoTestDO storage=wareHouseMap.get(storageName);
				if(storage==null){
					continue;
				}
				String materPicode=this.oftenMothedUtil.getStringCellValue(row.getCell(20));
				String pice=StringUtils.isEmpty(this.oftenMothedUtil.getStringCellValue(
						row.getCell(8)))?"0":
							this.oftenMothedUtil.getStringCellValue(row.getCell(8));
				String mun=StringUtils.isEmpty(this.oftenMothedUtil.getStringCellValue(
						row.getCell(15)))?"0":
							this.oftenMothedUtil.getStringCellValue(row.getCell(15));
				BigDecimal money=(new BigDecimal(pice)).multiply(new BigDecimal(mun));
				//code name model spec color quanlity storageCode picode
				String str=(StringUtils.isEmpty(materCode)?null:materCode)+"_"+
							(StringUtils.isEmpty(materName)?null:materName)+"_";
				String str2=(StringUtils.isEmpty(materModel)?null:materModel)+"_"+
							(StringUtils.isEmpty(materSpec)?null:materSpec)+"_";
				String str3=(StringUtils.isEmpty(materSpec)?null:materSpec)+"_"+
							(StringUtils.isEmpty(materModel)?null:materModel)+"_";
				String str4=(StringUtils.isEmpty(materColor)?null:materColor)+"_"+
							(StringUtils.isEmpty(materQuanlity)?null:materQuanlity)+"_"+
							(StringUtils.isEmpty(storage.getZd002Code())?null:storage.getZd002Code())+"_"+
							(StringUtils.isEmpty(materPicode)?null:materPicode);
				String key1=str+str2+str4;
				String key2=str+str3+str4;
				LogicExcelToStoreDO ex1 = getExcel(excelMap, materCode,
						materName, materModel, materSpec, materColor,
						materQuanlity, storage, materPicode, mun, money, key1);
				LogicExcelToStoreDO ex2 = getExcel(excelMap, materCode,
						materName, materModel, materSpec, materColor,
						materQuanlity, storage, materPicode, mun, money, key1);
				excelMap.put(key1, ex1);
				excelMap.put(key2, ex2);
				
			}
			try {
			String date=request.getParameter("zd008StepDate");
			SimpleDateFormat sdf= new SimpleDateFormat("yyyyMM");
			SearchConditions scs= this.oftenMothedUtil.getCleanCSearchCondition(new SearchConditions());
			scs.addCondition("zd008StepDate", sdf.parse(date), "INMONTH");
			List<LogicExcelToStoreDO> exList=this.logicExcelToStoreService.findListByExample(scs).getResults();
			if(!(exList.isEmpty()||exList.size()==0||exList==null)){
				this.logicExcelToStoreService.deleteLogicExcelToStoreByMonth(scs);
			}
			for(String key:excelMap.keySet()){
				LogicExcelToStoreDO ex=excelMap.get(key);
				ex.setId(IDCodeTypeUtil.getId());
					ex.setBizCreatorDate(sdf.parse(date));
				logicExcelToStoreService.save(ex, SessionUtil.getUser(request));
			}
			} catch (Exception e) {
				throw new SyBaseException("");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	private LogicExcelToStoreDO getExcel(
			Map<String, LogicExcelToStoreDO> excelMap, String materCode,
			String materName, String materModel, String materSpec,
			String materColor, String materQuanlity,
			BasicStorageInfoTestDO storage, String materPicode, String mun,
			BigDecimal money, String key1) {
		LogicExcelToStoreDO ex= excelMap.get(key1);
		if(ex==null){
			ex=new LogicExcelToStoreDO();
		}		
		ex.setZd001MaterialCode(materCode);
		ex.setZd001MaterialName(materName);
		ex.setZd002MaterialModel(materModel);
		ex.setZd003MaterialSpec(materSpec);
		ex.setZd003MaterialColor(materColor);
		ex.setZd003MaterialQuanlity(materQuanlity);
		ex.setZd012WareHouseCode(storage.getZd002Code());
		ex.setZd014MaterialPICode(materPicode);
		ex.setZd010MaterialEndNumber(new BigDecimal(mun).
				add(ex.getZd010MaterialEndNumber()==null?BigDecimal.ZERO:ex.getZd010MaterialEndNumber()));
		ex.setZd010MaterialEndMoney(money.
				add(ex.getZd010MaterialEndMoney()==null?BigDecimal.ZERO:ex.getZd010MaterialEndMoney()));
		return ex;
	}

}
